package zz.app.jmhviewer.JMH;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by zz on 2018/1/31 19:34.
 */

public class JMHCssParser {
    public String content;
    public JMHCssParser(String addr) throws IOException {
        URL url=new URL(addr);
        URLConnection connection = url.openConnection();
        InputStream inputStream= connection.getInputStream();
        StringBuilder builder= new StringBuilder();
        int i;
        byte[] buffer=new byte[1024];
        while((i=inputStream.read(buffer))>-1){
            builder.append(new String(buffer, 0, i));
        }
        content=builder.toString();
    }
    public String getValueByClassAndKey(String className,String keyName){
        String content= getClassContent(className);
        if(!content.equals("none")) {
            String trimString = content.replaceAll("\\s", "");
            String[] attrs=trimString.split(";");
            for(String s:attrs){
                String[] s1=s.split(":");
                if(s1.length>1&&s1[0].equals(keyName)){
                    return s1[1];
                }
            }
            return "none";
        }else{
            return "none";
        }
    }

    private String getClassContent(String className) {
        int i=content.indexOf(className);
        if(i==-1)return "none";
        String s=content.substring(i);
        int j=s.indexOf('{');
        int k=s.indexOf('}');
        return s.substring(j+1,k);


    }

}
